<template>
    <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width='600px'  :fullscreen = 'Global.currentDevice'   :append-to-body='true'> 
        <el-row class='mt12'>
            <el-form  label-width="120px" align='left' :model='form' :rules='rules'  ref='form'  :label-position='Global.dialog_label_Position'>
                <el-form-item label="审批意见："  class='mr30'  prop='advice'>
                    <el-input  type='textarea'  rows='5'  placeholder='同意/不同意/补充材料/其他说明，请输入'  v-model.trim='form.advice'></el-input>
                </el-form-item>
                <el-form-item label="添加文件：">
                    <el-upload
                      ref='upload'
                      :action="actionAddress"
                      :headers='upload_header'
                      multiple
                      accept='.txt,.doc,.ppt,.xls,.docx,.xlsx,.pptx,.jpg,.jpeg,.png,.pdf,.tiff,.swf,.flv,.rmvb,.mp4,.mvb,.zip,.rar'
                      :on-success="uploadOnSuccess"
                      :on-error="uploadOnError"
                      :before-upload="beforeAvatarUpload"
                      >
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="电子签名："  class='mr30'  prop='sign'>
                    <el-button type="primary" icon="el-icon-edit" circle  size='mini'  @click='showSignForm()'  v-if='sign_ele == true'></el-button>
                    <el-row  v-else>
                        <img  :src='signImg'  alt='电子签名图'  class='b-1-black'>
                    </el-row>
                    <el-button  v-if='sign_ele == false'  type="primary"   size='mini' @click='showSignForm()' >重签</el-button>
                    <el-button  v-if='sign_ele == false'  size='mini' @click='sign_ele = true' >取消</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <div slot="footer" class="dialog-footer">
            <el-button  class='export-icon'   round   @click="submit('form',1)">通 过</el-button>
            <el-button type="primary" round  @click="submit('form',0)">不通过</el-button>
            <el-button   round  @click="dialogProps.visible=false"  plain>取 消</el-button>
        </div>
        <!-- 电子签章 -->
        <signCanvas  ref='signForm' class='mt42'  v-on:save-finished='signFinish($event)'></signCanvas> 
    </el-dialog>
</template>

<script charset="utf-8" type="text/javascript">
  /*interface*/
import  { setup_wait_minister,setup_wait_legal,setup_wait_Manage,upSign }  from   '@/api/examine/index'

//电子签章
import  signCanvas  from  '@/components/common/signCanvas'
export default {
    name: 'handleRequest',
    data() {
        return {
            loading: false,
            //dialogo基本信息  显示隐藏状态  title 
            dialogProps: {
              visible: false,
              action: '',
              title: '审批'
            },
            //文件上传请求地址
            actionAddress:process.env.BASE_API+'/op/fileupload/upload_file',
            form:{
              advice:''
            },
            rules:{
              advice:[
                {required:true,message:'请输入审批信息'}
              ]
            },
            filelist:[],
            //文件上传请求地址
            actionAddress:process.env.BASE_API+'/op/fileupload/upload_file',
            /*上传请求头 加上token*/
             upload_header:{"X-Token":sessionStorage.getItem('token')},
            type:null,//审批类型 1立项申请  2.过会
            status:null,//审批状态 2.待风控审批
            currentCode:null,//当前需要审批的code

            signImg:'',//电子签名图 url
            sign_ele:true, //签名按钮 + 重签按钮 显示
            signImgCode:'',//电子签名code
        }
    },
    created(){
      
    },
    methods: {
        submit(formName,isPass){
            this.$refs[formName].validate(valid => {
                //通过验证后
                if (valid) {
                    let file_str=''
                    this.filelist.forEach((item)=>{
                        if(item.fileCode.length!=0){
                            file_str+=item.fileCode+','
                        }
                    })
                    file_str = file_str.slice(0,-1)

                    //部门负责人 
                    if(this.status==1){
                        let url = ''
                        if(this.type==1){
                            url = '/op/project_req/department_principal_audit'
                        }
                        if(this.type==2){
                            //项目过会申请
                            url = '/op/project_meeting_req/department_principal_audit'
                        }
                        if(this.type==3){
                            //协议用印申请
                            url = '/op/product_agreement_stamp_req/department_principal_audit'//部门负责人审批
                        }
                        if(this.type==4){
                            url = '/op/product_release_req/department_principal_audit'//部门负责人审批
                        }
                        //部门负责人
                        setup_wait_minister(url,this.currentCode,this.form.advice,file_str,isPass,this.signImgCode).then(res=>{
                            if(res.data.flag == true){
                                this.$emit('save-finished')
                                this.$message({type:'success',message:res.data.msg})
                                this.$router.push({path:'/examine'})
                            }
                        })
                        .catch(error=>{
                            
                            console.log(error)
      
                        })
                    }
                    //合规负责人审批
                    if(this.status==3){
                        let url = ''
                        if(this.type==1){
                            url = '/op/project_req/compliance_principal_audit'
                        }
                        if(this.type==2){
                           
                            url = '/op/project_meeting_req/compliance_principal_audit'
                        }
                        if(this.type==3){
                          
                            url = '/op/product_agreement_stamp_req/compliance_principal_audit'//合规负责人审批
                        }
                        if(this.type==4){
                            url = '/op/product_release_req/compliance_principal_audit'//合规负责人审批
                        }
                        

                        //合规负责人
                        setup_wait_legal(url,this.currentCode,this.form.advice,file_str,isPass,this.signImgCode).then(res=>{
                            if(res.data.flag == true){
                                this.$emit('save-finished')
                                this.$message({type:'success',message:res.data.msg})
                                this.$router.push({path:'/examine'})
                            }
                        })
                        .catch(error=>{
                            console.log(error)
                          
                        })
                    }

                    //执行总经理审批
                    if(this.status==5){
                        let url = ''
                        if(this.type==1){
                            url = '/op/project_req/executive_general_manager_audit'
                        }
                        if(this.type==2){
                            url = '/op/project_meeting_req/executive_general_manager_audit'
                        }
                        if(this.type==3){
                            url = '/op/product_agreement_stamp_req/executive_general_manager_audit'//执行总经理审批
                        }
                        if(this.type==4){
                            url = '/op/product_release_req/executive_general_manager_audit'//
                        }
                        

                        //执行总经理
                        setup_wait_Manage(url,this.currentCode,this.form.advice,file_str,isPass,this.signImgCode).then(res=>{
                            if(res.data.flag == true){
                                this.$emit('save-finished')
                                this.$message({type:'success',message:res.data.msg})
                                this.$router.push({path:'/examine'})
                            }
                        })
                        .catch(error=>{
                            
                            console.log(error)
      
                        })
                    }
                }
            })
        },
        uploadOnSuccess(response,file,fileList){
            console.log(file)
            this.filelist.push({fileName:file.response.data.name,fileCode:file.response.data.code})
            console.log(this.filelist)
        },
        //上传失败回调
        uploadOnError(e,file){
             
        },
        //before-upload 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
        beforeAvatarUpload(file){
            const isLt2M = file.size / 1024 / 1024 < 310;

            if (!isLt2M) {
                this.$message.error('上传文件大小不能超过 310MB!');
            }
            return  Boolean(isLt2M);
        },
        //显示电子签章
        showSignForm(){
            this.$refs.signForm.$emit('openDialog')
        },
        signFinish(e){
            // console.log(e)
            let blob = this.dataURLtoBlob(e);
            let file = this.blobToFile(blob);
            // console.log(file)
            let formData=new FormData()
            formData.append("file", file);
            // console.log(formData)
            upSign(formData).then(res=>{
                if(res.data.flag == true){
                    this.signImgCode= res.data.data.code
                }
            }).catch(error=>{
                console.log(error)
              })
            this.signImg = e
            this.sign_ele = false
        },
        //将base64转换为blob
        dataURLtoBlob(dataurl) { 
            let arr = dataurl.split(','),
                mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new Blob([u8arr], { type: mime });
        },
        //将blob转换为file
        blobToFile(theBlob){
            theBlob.lastModifiedDate = new Date();
            return theBlob;
        },
    },
    mounted: function() {
        this.$nextTick(() => {
            this.$on('openDialog', function(type,status,code) {
                this.dialogProps.visible = true
                this.type = type
                this.status = status
                this.currentCode = code
            })
          
        })
    },
    updated(){
      
    },
    components:{
      signCanvas
    }
}
</script>

<style  scoped>

</style>


